name: Release

on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.19

      - name: Тесты
        run: |
          go test -v ./... -gcflags=all=-l -race
        env:
          GO111MODULE: on

      - name: WinRes
        run: |
          go install github.com/tc-hib/go-winres
          go-winres make .\winres\winres.json

      - name: Get version
        id: get_version
        run: echo "version=${{ github.ref }}" | sed 's/refs\/tags\///' >> $GITHUB_ENV

      - name: Build for Linux
        run: GOOS=linux GOARCH=amd64 go build -o prometheus_1C_exporter-linux-amd64 -ldflags "-X main.version=${{ env.version }} -X main.gitCommit=${{ github.sha }}"

      - name: Build for Windows
        run: GOOS=windows GOARCH=amd64 go build -o prometheus_1C_exporter-windows-amd64.exe -ldflags "-X main.version=${{ env.version }} -X main.gitCommit=${{ github.sha }}"

      # получаем коммиты для описания релиза
      - name: Get commit messages
        id: commit_messages
        run: |
          PREVIOUS_TAG=$(git describe --abbrev=0 --tags $(git rev-list --tags --skip=2 -n 1))
          COMMITS=$(git log --pretty=format:"- %s (%h)" $PREVIOUS_TAG..HEAD)
          echo "$PREVIOUS_TAG"
          echo "::set-output name=commits::$COMMITS"

      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            Release ${{ github.ref }} is available.
            
            Commits:
            ${{ steps.commit_messages.outputs.commits }}
          draft: false
          prerelease: false

      - name: Upload binaries
        uses: actions/upload-artifact@v2
        with:
          name: Binaries
          path: |
            prometheus_1C_exporter-linux-amd64
            prometheus_1C_exporter-windows-amd64.exe


      - name: Download binaries
        uses: actions/download-artifact@v2
        with:
          name: Binaries
          path: prometheus_1C_exporter-binaries

      - name: Attach binaries to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: prometheus_1C_exporter-binaries/prometheus_1C_exporter-linux-amd64
          asset_name: prometheus_1C_exporter-linux-amd64
          asset_content_type: application/octet-stream

      - name: Attach binaries to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: prometheus_1C_exporter-binaries/prometheus_1C_exporter-windows-amd64.exe
          asset_name: prometheus_1C_exporter-windows-amd64.exe
          asset_content_type: application/octet-stream